﻿namespace Yipusoft.Account.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Yipusoft.Account.DBUtility;
    using Yipusoft.Account.Model;

    public class PermissionCategory
    {
        public bool Delete(string CategoryID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = CategoryID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_PermissionCategories_Delete", commandParameters) > 0);
            }
        }

        public bool Exsits(string CategoryID)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = CategoryID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_PermissionCategories_Exsits", commandParameters).Read();
            }
        }

        public List<PermissionCategoryInfo> GetList(string StrWhere)
        {
            List<PermissionCategoryInfo> list = new List<PermissionCategoryInfo>();
            PermissionCategoryInfo item = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@StrWhere", SqlDbType.NVarChar, 0xfa0) };
            commandParameters[0].Value = StrWhere;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_PermissionCategories_GetList", commandParameters);
                while (reader.Read())
                {
                    item = new PermissionCategoryInfo();
                    item.CategoryID = reader.GetString(0);
                    item.CategoryName = reader.GetString(1);
                    item.Description = reader.GetString(2);
                    list.Add(item);
                }
                return list;
            }
        }

        public PermissionCategoryInfo GetPermissionCategory(string CategoryID)
        {
            PermissionCategoryInfo info = null;
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = CategoryID;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader reader = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Account_PermissionCategories_GetDetail", commandParameters);
                if (reader.Read())
                {
                    info = new PermissionCategoryInfo();
                    info.CategoryID = reader.GetString(0);
                    info.CategoryName = reader.GetString(1);
                    info.Description = reader.GetString(2);
                }
                return info;
            }
        }

        public bool Insert(PermissionCategoryInfo PermissionCategory)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.NVarChar, 50), new SqlParameter("@CategoryName", SqlDbType.NVarChar, 0xff), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = PermissionCategory.CategoryID;
            commandParameters[1].Value = PermissionCategory.CategoryName;
            commandParameters[2].Value = PermissionCategory.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_PermissionCategories_Insert", commandParameters) > 0);
            }
        }

        public bool Update(PermissionCategoryInfo PermissionCategory)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.NVarChar, 50), new SqlParameter("@CategoryName", SqlDbType.NVarChar, 0xff), new SqlParameter("@Description", SqlDbType.NVarChar, 0xff) };
            commandParameters[0].Value = PermissionCategory.CategoryID;
            commandParameters[1].Value = PermissionCategory.CategoryName;
            commandParameters[2].Value = PermissionCategory.Description;
            using (SqlConnection connection = new SqlConnection(SqlHelper.CONN_STRING))
            {
                return (SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "Account_PermissionCategories_Update", commandParameters) > 0);
            }
        }
    }
}

